package com.zhuge_new.Activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.maning.mndialoglibrary.MToast;
import com.santalu.maskedittext.MaskEditText;
import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.R;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.ActivityStack;
import com.zhuge_new.untils.PreferencesUtils;
import com.zhuge_new.untils.TimerUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by lenovo on 2018-08-30.
 */

public class CreditAddition3Activity extends BaseActivity implements View.OnClickListener {

    private String bank_number;
    private EditText cell_phone;
    private String cvn;
    private String ptiod;
    private TextView bt_yzm;
    private String smsCode;
    private MaskEditText verification_code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_creditadd3ition);
        init_title();
    }

    @Override
    public void init_title() {
        super.init_title();
        TextView tv_nav_title=findViewById(R.id.tv_nav_title);
        tv_nav_title.setText("资金效验");
        ImageView iv_nav_back=findViewById(R.id.iv_nav_back);
        iv_nav_back.setOnClickListener(this);
        Intent intent = getIntent();
        ptiod = intent.getStringExtra("ptiod");
        cvn = intent.getStringExtra("cvn");
        bank_number = intent.getStringExtra("bank_number");


        cell_phone = findViewById(R.id.cell_phone);
        bt_yzm = findViewById(R.id.bt_yzm);
        verification_code = findViewById(R.id.verification_code);

        TextView accomplish = findViewById(R.id.accomplish);
        bt_yzm.setOnClickListener(this);
        accomplish.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_yzm:
                init_yzm();
                break;
            case R.id.accomplish:
                init_add();
                break;
            case R.id.iv_nav_back:
                finish();
                break;
        }
    }

    private void init_yzm() {
        if (cell_phone.getText().toString().length()!=11){
            MToast.makeTextShort(CreditAddition3Activity.this,"请输入正确手机号码").show();
        }else {
            TimerUtil timerUtil = new TimerUtil(bt_yzm, 60000, 1000);
            timerUtil.start();
            OkGo.<String>post(BaseHttp.identify_othercode)
                    .tag(this)
                    .headers("tokne", PreferencesUtils.getString(baseContext,"token"))
                    .params("mobile", cell_phone.getText().toString())
                    .params("type", "3")
                    .execute(new StringDialogCallback(baseContext) {

                        @Override
                        public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                            try {
                                smsCode = new JSONObject(response.body()).getString("object");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
        }

    }

    private void init_add() {
        if (verification_code.getText().toString().equals(smsCode)) {
            OkGo.<String>post(BaseHttp.creditcard_add_sub)
                    .tag(this)
                    .headers("token", PreferencesUtils.getString(baseContext,"token"))
                    .params("creditcard", bank_number)
                    .params("smsCode", smsCode)
                    .params("invalidDate", ptiod)
                    .params("scn2", cvn)
                    .params("tel", cell_phone.getText().toString())
                    .params("name", PreferencesUtils.getString(this, "name"))
                    .params("identityCard", PreferencesUtils.getString(this, "id_number"))
                    .params("type", "0")
                    .execute(new StringDialogCallback(baseContext) {
                        @Override
                        public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                            MToast.makeTextShort(CreditAddition3Activity.this,"绑定成功").show();
                            ActivityStack.getScreenManager().popActivities(CreditAddition1Activity.class,CreditAddition2Activity.class,CreditAddition3Activity.class);
                        }
                    });
        }else {
            MToast.makeTextShort(CreditAddition3Activity.this,"请输入正确验证码").show();

        }
    }
}
